package com.jlastu.znb.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;

public class ModifyingInformationActivity extends AppCompatActivity {
    private ImageView mBack;
    private EditText mInputName;
    private TextView mSave;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modifying_information);
        mBack = findViewById(R.id.back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mSave = findViewById(R.id.save);
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mInputName = findViewById(R.id.et_goods_name);
                String inputText = mInputName.getText().toString(); // 获取EditText中的内容

                // 将获取的内容保存到SharedPreferences
                SharedPreferences sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("NAME_KEY", inputText);
                editor.apply();

                // 将获取的内容发送回上一个Activity
                Intent data = new Intent();
                data.putExtra("NAME_KEY", inputText);
                setResult(RESULT_OK, data);
                finish();
            }
        });

        // 从SharedPreferences获取名字，并设置到EditText
        SharedPreferences sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        String savedName = sharedPreferences.getString("NAME_KEY", ""); // 获取SharedPreferences中的名字
        mInputName = findViewById(R.id.et_goods_name);
        mInputName.setText(savedName); // 设置EditText的文本
        mInputName.setSelection(mInputName.length()); // 将光标移动到文本末尾
    }
}